package com.lanzhou.yuanfen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanzhou.yuanfen.config.Token;
import com.lanzhou.yuanfen.response.ServerResponseResult;
import com.lanzhou.yuanfen.sys.entity.User;
import com.lanzhou.yuanfen.tool.entity.AliExpressTable;
import com.lanzhou.yuanfen.tool.service.IAliExpressTableService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created on 2020/5/24 11:12
 * Code By LanZhou
 *
 * @author LanZhou
 */
@RestController
@RequestMapping("/tool/ali/size/")
public class AliSizeController {

    @Resource
    private IAliExpressTableService aliExpressTableService;

    /**
     * 获取尺码表
     *
     * @return
     */
    @PostMapping("getSizeTables")
    public ServerResponseResult getSizeTable() {
        User userInfo = Token.getUserInfo();
        if (userInfo == null) {
            return ServerResponseResult.fail("当前暂无用户登入");
        }
        Long userKey = userInfo.getUserKey();
        List<AliExpressTable> sizeTable = aliExpressTableService
                .list(new QueryWrapper<AliExpressTable>()
                        .eq("create_by", userKey)
                        .orderByDesc("update_time", "create_time"));
        return ServerResponseResult.success(sizeTable);
    }


    /**
     * 添加文章/修改文章
     *
     * @param table
     * @return
     */
    @PostMapping("addOrUpdateSizeTable")
    public ServerResponseResult addOrUpdateSizeTable(AliExpressTable table) {
        boolean save = aliExpressTableService.saveOrUpdate(table);
        return save ? ServerResponseResult.success(table) : ServerResponseResult.fail();
    }


}
